{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-11-05T22:54:56.919719Z",
     "start_time": "2025-11-05T22:54:56.913716Z"
    }
   },
   "source": "## 2.1 数据操作",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-05T22:55:04.515335Z",
     "start_time": "2025-11-05T22:55:03.254221Z"
    }
   },
   "cell_type": "code",
   "source": "import torch",
   "id": "2d5d3794dddb4ff0",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-05T22:55:28.567823Z",
     "start_time": "2025-11-05T22:55:28.541160Z"
    }
   },
   "cell_type": "code",
   "source": "print(\"Judge wheather torch cuda is available:\", torch.cuda.is_available())",
   "id": "119b432c5098b446",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Judge wheather torch cuda is available True\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-05T22:55:51.671018Z",
     "start_time": "2025-11-05T22:55:51.437737Z"
    }
   },
   "cell_type": "code",
   "source": "x = torch.rand(3, 3, 3, 3)",
   "id": "90b5ae61afab7230",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-05T22:55:52.508919Z",
     "start_time": "2025-11-05T22:55:52.493906Z"
    }
   },
   "cell_type": "code",
   "source": "x",
   "id": "60019cf3ca44db4b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[[5.9808e-03, 7.4044e-01, 3.1217e-01],\n",
       "          [2.4829e-01, 8.6889e-01, 2.9841e-01],\n",
       "          [5.8582e-02, 4.5697e-01, 6.7571e-01]],\n",
       "\n",
       "         [[2.1790e-01, 1.5401e-01, 5.9177e-01],\n",
       "          [8.7035e-01, 5.9155e-01, 7.2045e-01],\n",
       "          [1.9344e-02, 1.7162e-01, 8.2057e-01]],\n",
       "\n",
       "         [[3.2344e-01, 4.6318e-01, 3.7862e-01],\n",
       "          [1.5637e-01, 6.1219e-01, 2.2563e-01],\n",
       "          [6.6424e-01, 1.2230e-01, 3.4330e-01]]],\n",
       "\n",
       "\n",
       "        [[[4.6402e-01, 1.8639e-01, 5.1911e-01],\n",
       "          [6.2656e-04, 9.0581e-01, 8.8342e-01],\n",
       "          [3.7470e-01, 3.7112e-01, 2.9528e-01]],\n",
       "\n",
       "         [[3.5470e-01, 6.6634e-01, 1.8966e-01],\n",
       "          [7.4480e-01, 8.0226e-01, 5.3270e-01],\n",
       "          [4.5432e-01, 1.6754e-01, 8.8955e-01]],\n",
       "\n",
       "         [[8.3954e-02, 7.3379e-01, 2.5081e-01],\n",
       "          [2.8631e-01, 2.4658e-01, 3.7618e-01],\n",
       "          [8.3167e-01, 2.2508e-01, 1.1914e-01]]],\n",
       "\n",
       "\n",
       "        [[[6.3452e-01, 6.1896e-01, 9.3696e-03],\n",
       "          [2.9830e-01, 4.5253e-01, 3.7246e-01],\n",
       "          [9.1679e-02, 2.5033e-01, 8.7834e-02]],\n",
       "\n",
       "         [[6.3274e-01, 4.0536e-01, 9.3049e-01],\n",
       "          [3.7592e-01, 7.8162e-01, 2.7820e-01],\n",
       "          [2.8348e-02, 6.3800e-01, 6.1355e-01]],\n",
       "\n",
       "         [[5.1724e-01, 2.0601e-01, 4.2540e-01],\n",
       "          [8.0372e-01, 3.5411e-01, 3.3120e-01],\n",
       "          [4.7699e-01, 9.4201e-01, 8.5403e-01]]]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-05T22:56:12.672340Z",
     "start_time": "2025-11-05T22:56:12.248103Z"
    }
   },
   "cell_type": "code",
   "source": "y = torch.arange(3, dtype=torch.long, device=torch.device('cuda'))",
   "id": "5351d0f57831d0c0",
   "outputs": [],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-05T22:56:13.675563Z",
     "start_time": "2025-11-05T22:56:13.659549Z"
    }
   },
   "cell_type": "code",
   "source": "y",
   "id": "b8fcfee318655a17",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0, 1, 2], device='cuda:0')"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-05T22:56:32.672089Z",
     "start_time": "2025-11-05T22:56:32.609393Z"
    }
   },
   "cell_type": "code",
   "source": "z = torch.arange(12)",
   "id": "4c10c77b6162afc7",
   "outputs": [],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-05T22:56:34.656752Z",
     "start_time": "2025-11-05T22:56:34.652223Z"
    }
   },
   "cell_type": "code",
   "source": "z",
   "id": "cb475a981e80f779",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-05T22:56:48.294732Z",
     "start_time": "2025-11-05T22:56:48.286685Z"
    }
   },
   "cell_type": "code",
   "source": "z.shape",
   "id": "25a6bb9a763f93f6",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([12])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T15:35:10.313842Z",
     "start_time": "2025-11-06T15:35:10.184913Z"
    }
   },
   "cell_type": "code",
   "source": "torch.zeros((2,3,4))",
   "id": "9ac5f3391836d15",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0., 0., 0., 0.],\n",
       "         [0., 0., 0., 0.],\n",
       "         [0., 0., 0., 0.]],\n",
       "\n",
       "        [[0., 0., 0., 0.],\n",
       "         [0., 0., 0., 0.],\n",
       "         [0., 0., 0., 0.]]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T15:35:32.311435Z",
     "start_time": "2025-11-06T15:35:32.262611Z"
    }
   },
   "cell_type": "code",
   "source": "torch.ones((2,3,4))",
   "id": "ed53a608d05a42da",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[1., 1., 1., 1.],\n",
       "         [1., 1., 1., 1.],\n",
       "         [1., 1., 1., 1.]],\n",
       "\n",
       "        [[1., 1., 1., 1.],\n",
       "         [1., 1., 1., 1.],\n",
       "         [1., 1., 1., 1.]]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T15:37:26.350313Z",
     "start_time": "2025-11-06T15:37:26.334145Z"
    }
   },
   "cell_type": "code",
   "source": "t_a = torch.randn(3,4)",
   "id": "d02d9eb208414de2",
   "outputs": [],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T15:37:29.737589Z",
     "start_time": "2025-11-06T15:37:29.724087Z"
    }
   },
   "cell_type": "code",
   "source": "t_a",
   "id": "8ce29150ee3679cb",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.4264,  1.9143,  1.3527,  0.3820],\n",
       "        [ 0.3034,  0.4929, -0.4416, -1.0407],\n",
       "        [ 0.7485,  0.0383,  0.1826,  0.3232]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T15:38:10.966319Z",
     "start_time": "2025-11-06T15:38:10.947079Z"
    }
   },
   "cell_type": "code",
   "source": "torch.mean(t_a[0])",
   "id": "d877a6102895ec4",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(1.0188)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T15:38:24.764513Z",
     "start_time": "2025-11-06T15:38:24.749498Z"
    }
   },
   "cell_type": "code",
   "source": "t_a[0]",
   "id": "ae711c346f827bca",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0.4264, 1.9143, 1.3527, 0.3820])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T15:55:59.495574Z",
     "start_time": "2025-11-06T15:55:59.490574Z"
    }
   },
   "cell_type": "code",
   "source": "## 2.1.2 运算符",
   "id": "c9b64544fca5a6cb",
   "outputs": [],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T15:56:31.993397Z",
     "start_time": "2025-11-06T15:56:31.918445Z"
    }
   },
   "cell_type": "code",
   "source": "x = torch.tensor([1.0,2,4,8])",
   "id": "3f2637296d3d41e7",
   "outputs": [],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T15:56:48.061144Z",
     "start_time": "2025-11-06T15:56:48.047569Z"
    }
   },
   "cell_type": "code",
   "source": "y = torch.tensor([2,2,2,2])",
   "id": "c8f984ec764221d",
   "outputs": [],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T15:57:00.003053Z",
     "start_time": "2025-11-06T15:56:59.896990Z"
    }
   },
   "cell_type": "code",
   "source": "c = x+y",
   "id": "167dd6e167d031c0",
   "outputs": [],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T15:57:01.056761Z",
     "start_time": "2025-11-06T15:57:01.041751Z"
    }
   },
   "cell_type": "code",
   "source": "c",
   "id": "eabaeee27ccc840c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 3.,  4.,  6., 10.])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T15:57:12.279643Z",
     "start_time": "2025-11-06T15:57:12.231613Z"
    }
   },
   "cell_type": "code",
   "source": "x - y",
   "id": "e40d6d0dbe5a60e3",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([-1.,  0.,  2.,  6.])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T15:57:19.043660Z",
     "start_time": "2025-11-06T15:57:18.995862Z"
    }
   },
   "cell_type": "code",
   "source": "x*y",
   "id": "af0af712b986afee",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 2.,  4.,  8., 16.])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T15:57:26.440660Z",
     "start_time": "2025-11-06T15:57:26.433474Z"
    }
   },
   "cell_type": "code",
   "source": "x/y",
   "id": "219cbd8a64442b64",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0.5000, 1.0000, 2.0000, 4.0000])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T15:57:31.016296Z",
     "start_time": "2025-11-06T15:57:30.972595Z"
    }
   },
   "cell_type": "code",
   "source": "x**y",
   "id": "d0356093e3952817",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 1.,  4., 16., 64.])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T15:57:48.448795Z",
     "start_time": "2025-11-06T15:57:48.326028Z"
    }
   },
   "cell_type": "code",
   "source": "torch.exp(x)",
   "id": "b33611392e75cff0",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([2.7183e+00, 7.3891e+00, 5.4598e+01, 2.9810e+03])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T15:58:03.381259Z",
     "start_time": "2025-11-06T15:58:03.367247Z"
    }
   },
   "cell_type": "code",
   "source": "x==y",
   "id": "4c580c2a80a2345f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([False,  True, False, False])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T23:02:52.577090Z",
     "start_time": "2025-11-06T23:02:52.562586Z"
    }
   },
   "cell_type": "code",
   "source": "## 2.1.3 广播机制",
   "id": "3465f002e0478fd9",
   "outputs": [],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T23:03:17.880490Z",
     "start_time": "2025-11-06T23:03:17.874990Z"
    }
   },
   "cell_type": "code",
   "source": "a = torch.arange(3).reshape((3,1))",
   "id": "8bb70686ca6c6039",
   "outputs": [],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T23:03:19.095142Z",
     "start_time": "2025-11-06T23:03:19.082638Z"
    }
   },
   "cell_type": "code",
   "source": "a",
   "id": "456e20cdb30486c2",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0],\n",
       "        [1],\n",
       "        [2]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T23:03:41.224331Z",
     "start_time": "2025-11-06T23:03:41.214827Z"
    }
   },
   "cell_type": "code",
   "source": "b = torch.arange(2).reshape((1,2))",
   "id": "cd7da6ebf26afa33",
   "outputs": [],
   "execution_count": 34
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T23:03:42.503723Z",
     "start_time": "2025-11-06T23:03:42.497601Z"
    }
   },
   "cell_type": "code",
   "source": "b",
   "id": "3558fe220bf5dbf7",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 1]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T23:03:58.335138Z",
     "start_time": "2025-11-06T23:03:58.288597Z"
    }
   },
   "cell_type": "code",
   "source": "a+b",
   "id": "658dde30f38dedcd",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 1],\n",
       "        [1, 2],\n",
       "        [2, 3]])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 36
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T23:05:34.931540Z",
     "start_time": "2025-11-06T23:05:34.927499Z"
    }
   },
   "cell_type": "code",
   "source": "## 2.1.4 索引与切片",
   "id": "624af0ae00fb7d40",
   "outputs": [],
   "execution_count": 37
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T23:07:20.330767Z",
     "start_time": "2025-11-06T23:07:20.310078Z"
    }
   },
   "cell_type": "code",
   "source": "X = torch.arange(12,dtype=torch.float32).reshape((3,4))",
   "id": "38ec0d65017c43ab",
   "outputs": [],
   "execution_count": 40
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T23:07:30.017012Z",
     "start_time": "2025-11-06T23:07:30.008314Z"
    }
   },
   "cell_type": "code",
   "source": "X",
   "id": "5623f7e855e43562",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.,  1.,  2.,  3.],\n",
       "        [ 4.,  5.,  6.,  7.],\n",
       "        [ 8.,  9., 10., 11.]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 41
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T23:07:41.467361Z",
     "start_time": "2025-11-06T23:07:41.454289Z"
    }
   },
   "cell_type": "code",
   "source": "X[-1]",
   "id": "4fdf34d703b77c86",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 8.,  9., 10., 11.])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 42
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T23:07:51.437958Z",
     "start_time": "2025-11-06T23:07:51.431215Z"
    }
   },
   "cell_type": "code",
   "source": "X[0]",
   "id": "58189426e6eb5138",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0., 1., 2., 3.])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 43
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T23:08:04.957786Z",
     "start_time": "2025-11-06T23:08:04.921411Z"
    }
   },
   "cell_type": "code",
   "source": "X[1,2]",
   "id": "38f0b268c640e530",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(6.)"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 44
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T23:08:32.932410Z",
     "start_time": "2025-11-06T23:08:32.922387Z"
    }
   },
   "cell_type": "code",
   "source": "X[0,2]",
   "id": "ee7032ac388a8f58",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(2.)"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 45
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-07T22:47:44.582406Z",
     "start_time": "2025-11-07T22:47:44.567376Z"
    }
   },
   "cell_type": "code",
   "source": "X",
   "id": "1f6042057fcc4ec5",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.,  1.,  2.,  3.],\n",
       "        [ 4.,  5.,  6.,  7.],\n",
       "        [ 8.,  9., 10., 11.]])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 46
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-07T22:48:13.328523Z",
     "start_time": "2025-11-07T22:48:13.298887Z"
    }
   },
   "cell_type": "code",
   "source": "X[1,2]=9",
   "id": "98e3d302c868ac44",
   "outputs": [],
   "execution_count": 47
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-07T22:48:16.610797Z",
     "start_time": "2025-11-07T22:48:16.602367Z"
    }
   },
   "cell_type": "code",
   "source": "X",
   "id": "a30cd02f00638c75",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.,  1.,  2.,  3.],\n",
       "        [ 4.,  5.,  9.,  7.],\n",
       "        [ 8.,  9., 10., 11.]])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 48
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-07T22:49:06.797617Z",
     "start_time": "2025-11-07T22:49:06.766402Z"
    }
   },
   "cell_type": "code",
   "source": "X[0:2,:]=12",
   "id": "d29ce09e34445d73",
   "outputs": [],
   "execution_count": 49
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-07T22:49:08.796910Z",
     "start_time": "2025-11-07T22:49:08.782403Z"
    }
   },
   "cell_type": "code",
   "source": "X",
   "id": "99f91891258e9fbb",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[12., 12., 12., 12.],\n",
       "        [12., 12., 12., 12.],\n",
       "        [ 8.,  9., 10., 11.]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 50
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-07T22:50:11.745431Z",
     "start_time": "2025-11-07T22:50:11.724443Z"
    }
   },
   "cell_type": "code",
   "source": "## 2.1.5 节省内存",
   "id": "f55956377b604065",
   "outputs": [],
   "execution_count": 51
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-07T22:50:45.923579Z",
     "start_time": "2025-11-07T22:50:45.909574Z"
    }
   },
   "cell_type": "code",
   "source": "before = id(X)",
   "id": "9af068dc1091974c",
   "outputs": [],
   "execution_count": 53
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-07T22:51:18.583564Z",
     "start_time": "2025-11-07T22:51:18.569183Z"
    }
   },
   "cell_type": "code",
   "source": "Y = X+X",
   "id": "70cdf841fc97ab22",
   "outputs": [],
   "execution_count": 54
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-07T22:52:03.607187Z",
     "start_time": "2025-11-07T22:52:03.601659Z"
    }
   },
   "cell_type": "code",
   "source": "id(X) == before",
   "id": "72569133816883da",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 56
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-10T22:51:05.468574Z",
     "start_time": "2025-11-10T22:51:05.459533Z"
    }
   },
   "cell_type": "code",
   "source": "X",
   "id": "de88ba8f5ae2b0fc",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[12., 12., 12., 12.],\n",
       "        [12., 12., 12., 12.],\n",
       "        [ 8.,  9., 10., 11.]])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 57
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-10T22:51:32.237438Z",
     "start_time": "2025-11-10T22:51:32.223662Z"
    }
   },
   "cell_type": "code",
   "source": "## 2.1.6 转换为其它Python对象",
   "id": "7ec5954686003e8",
   "outputs": [],
   "execution_count": 58
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-10T22:51:41.700082Z",
     "start_time": "2025-11-10T22:51:41.686295Z"
    }
   },
   "cell_type": "code",
   "source": "A = X.numpy()",
   "id": "252f198beddaeea8",
   "outputs": [],
   "execution_count": 59
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-10T22:51:42.827226Z",
     "start_time": "2025-11-10T22:51:42.804102Z"
    }
   },
   "cell_type": "code",
   "source": "A",
   "id": "ea57d90f517c52b8",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[12., 12., 12., 12.],\n",
       "       [12., 12., 12., 12.],\n",
       "       [ 8.,  9., 10., 11.]], dtype=float32)"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 60
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-10T22:51:47.245723Z",
     "start_time": "2025-11-10T22:51:47.232328Z"
    }
   },
   "cell_type": "code",
   "source": "X",
   "id": "7ecfd2000350ff2f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[12., 12., 12., 12.],\n",
       "        [12., 12., 12., 12.],\n",
       "        [ 8.,  9., 10., 11.]])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 61
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-10T22:51:53.031177Z",
     "start_time": "2025-11-10T22:51:53.017163Z"
    }
   },
   "cell_type": "code",
   "source": "A",
   "id": "456119b84f0afae3",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[12., 12., 12., 12.],\n",
       "       [12., 12., 12., 12.],\n",
       "       [ 8.,  9., 10., 11.]], dtype=float32)"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 62
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-10T22:52:06.903661Z",
     "start_time": "2025-11-10T22:52:06.897447Z"
    }
   },
   "cell_type": "code",
   "source": "## 对比以上X和A的区别 ",
   "id": "37ebd773806047c7",
   "outputs": [],
   "execution_count": 63
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-10T22:52:10.223619Z",
     "start_time": "2025-11-10T22:52:10.217604Z"
    }
   },
   "cell_type": "code",
   "source": "type(X)",
   "id": "efcb02f32eff7a0b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Tensor"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 64
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-10T22:52:14.864856Z",
     "start_time": "2025-11-10T22:52:14.857884Z"
    }
   },
   "cell_type": "code",
   "source": "type(A)",
   "id": "31b011c0d047c014",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 65
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-10T22:52:46.219693Z",
     "start_time": "2025-11-10T22:52:46.204506Z"
    }
   },
   "cell_type": "code",
   "source": "# X是张量，A是数组",
   "id": "ad8dfef892e0137b",
   "outputs": [],
   "execution_count": 66
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-10T22:52:49.990749Z",
     "start_time": "2025-11-10T22:52:49.986040Z"
    }
   },
   "cell_type": "code",
   "source": "X.shape",
   "id": "5ee50f4292bf8459",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([3, 4])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 67
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-10T22:52:58.988402Z",
     "start_time": "2025-11-10T22:52:58.973424Z"
    }
   },
   "cell_type": "code",
   "source": "A.shape",
   "id": "32534009f5d12bcf",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 4)"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 68
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-10T22:53:33.433598Z",
     "start_time": "2025-11-10T22:53:33.372597Z"
    }
   },
   "cell_type": "code",
   "source": "B = torch.tensor(A)",
   "id": "5d3ab9054046c06f",
   "outputs": [],
   "execution_count": 69
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-10T22:53:34.819742Z",
     "start_time": "2025-11-10T22:53:34.800737Z"
    }
   },
   "cell_type": "code",
   "source": "B",
   "id": "19ec6324bc98cf49",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[12., 12., 12., 12.],\n",
       "        [12., 12., 12., 12.],\n",
       "        [ 8.,  9., 10., 11.]])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 70
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-10T22:53:38.903505Z",
     "start_time": "2025-11-10T22:53:38.898Z"
    }
   },
   "cell_type": "code",
   "source": "type(B)",
   "id": "434de0eeb41aa324",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Tensor"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 71
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-10T22:55:30.935543Z",
     "start_time": "2025-11-10T22:55:30.929031Z"
    }
   },
   "cell_type": "code",
   "source": "a=torch.tensor([3.5])",
   "id": "cef0d4a493a10f3",
   "outputs": [],
   "execution_count": 77
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-10T22:55:31.694325Z",
     "start_time": "2025-11-10T22:55:31.687816Z"
    }
   },
   "cell_type": "code",
   "source": "a",
   "id": "38cea2700afc9b16",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([3.5000])"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 78
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-10T22:55:34.994510Z",
     "start_time": "2025-11-10T22:55:34.989718Z"
    }
   },
   "cell_type": "code",
   "source": "a.item()",
   "id": "499617c3377b256",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.5"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 79
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-10T22:55:36.470068Z",
     "start_time": "2025-11-10T22:55:36.453975Z"
    }
   },
   "cell_type": "code",
   "source": "float(a)",
   "id": "df3f868c632597fa",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.5"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 80
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-10T22:55:46.492809Z",
     "start_time": "2025-11-10T22:55:46.481488Z"
    }
   },
   "cell_type": "code",
   "source": "int(a)",
   "id": "cb19390b507c7742",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 81
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-10T22:55:49.351791Z",
     "start_time": "2025-11-10T22:55:49.342194Z"
    }
   },
   "cell_type": "code",
   "source": "str(a)",
   "id": "e9c494f7d1450035",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'tensor([3.5000])'"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 82
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-13T23:07:02.953032Z",
     "start_time": "2025-11-13T23:07:02.944477Z"
    }
   },
   "cell_type": "code",
   "source": "import torch",
   "id": "635c65f71da8a8a5",
   "outputs": [],
   "execution_count": 83
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-13T23:08:56.970096Z",
     "start_time": "2025-11-13T23:08:56.959284Z"
    }
   },
   "cell_type": "code",
   "source": "X,y = torch.tensor([2,3]),torch.randn([2,3])",
   "id": "710c7a9c3d89a8a5",
   "outputs": [],
   "execution_count": 87
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-13T23:09:05.136731Z",
     "start_time": "2025-11-13T23:09:05.119605Z"
    }
   },
   "cell_type": "code",
   "source": "X,y",
   "id": "cccc3f72e6cf6b14",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([2, 3]),\n",
       " tensor([[ 0.7708,  1.7015,  0.2002],\n",
       "         [ 0.8233,  0.7762, -2.0027]]))"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 88
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-14T15:32:12.932035Z",
     "start_time": "2025-11-14T15:32:12.929482Z"
    }
   },
   "cell_type": "code",
   "source": "## 2.5 自动微分",
   "id": "87f37c9a8ca33205",
   "outputs": [],
   "execution_count": 89
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-14T15:36:32.991722Z",
     "start_time": "2025-11-14T15:36:32.988220Z"
    }
   },
   "cell_type": "code",
   "source": "import torch",
   "id": "b7ba4dc9d8358385",
   "outputs": [],
   "execution_count": 102
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-14T15:36:33.528421Z",
     "start_time": "2025-11-14T15:36:33.524421Z"
    }
   },
   "cell_type": "code",
   "source": "x = torch.arange(4.0)",
   "id": "cb2e57018ad8b06d",
   "outputs": [],
   "execution_count": 103
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-14T15:36:34.378440Z",
     "start_time": "2025-11-14T15:36:34.364370Z"
    }
   },
   "cell_type": "code",
   "source": "x",
   "id": "82316782c6641c44",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0., 1., 2., 3.])"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 104
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-14T15:37:36.514717Z",
     "start_time": "2025-11-14T15:37:36.510630Z"
    }
   },
   "cell_type": "code",
   "source": "x.requires_grad_(True)",
   "id": "b4a2c20eed08f2d0",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0., 1., 2., 3.], requires_grad=True)"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 109
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-14T15:37:41.972081Z",
     "start_time": "2025-11-14T15:37:41.966402Z"
    }
   },
   "cell_type": "code",
   "source": "x.grad",
   "id": "b35aa993b034d5e8",
   "outputs": [],
   "execution_count": 110
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-14T15:38:06.700913Z",
     "start_time": "2025-11-14T15:38:06.678160Z"
    }
   },
   "cell_type": "code",
   "source": "y = 2*torch.dot(x,x)",
   "id": "a94b178daf044b14",
   "outputs": [],
   "execution_count": 111
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-14T15:38:10.057698Z",
     "start_time": "2025-11-14T15:38:10.043124Z"
    }
   },
   "cell_type": "code",
   "source": "y",
   "id": "eda7f9bd4c7da118",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(28., grad_fn=<MulBackward0>)"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 112
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-14T15:38:30.553294Z",
     "start_time": "2025-11-14T15:38:30.420401Z"
    }
   },
   "cell_type": "code",
   "source": "y.backward()",
   "id": "1f1ac433f7049956",
   "outputs": [],
   "execution_count": 113
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-14T15:38:35.278739Z",
     "start_time": "2025-11-14T15:38:35.269234Z"
    }
   },
   "cell_type": "code",
   "source": "x.grad",
   "id": "ee73c66ed0ee6b74",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 0.,  4.,  8., 12.])"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 114
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-14T15:38:50.155893Z",
     "start_time": "2025-11-14T15:38:50.150800Z"
    }
   },
   "cell_type": "code",
   "source": "x.grad == 4*x",
   "id": "19152605c3b2c34a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([True, True, True, True])"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 115
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-14T15:39:29.151829Z",
     "start_time": "2025-11-14T15:39:29.080633Z"
    }
   },
   "cell_type": "code",
   "source": [
    "x.grad.zero_()\n",
    "y = x.sum()\n",
    "y.backward()\n",
    "x.grad"
   ],
   "id": "24723b11a7af6f08",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1., 1., 1., 1.])"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 116
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-14T15:36:05.734757Z",
     "start_time": "2025-11-14T15:36:04.968667Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "9b73bf74fb4d67d2",
   "outputs": [
    {
     "ename": "RuntimeError",
     "evalue": "element 0 of tensors does not require grad and does not have a grad_fn",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mRuntimeError\u001B[0m                              Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[101], line 1\u001B[0m\n\u001B[1;32m----> 1\u001B[0m \u001B[43my\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mbackward\u001B[49m\u001B[43m(\u001B[49m\u001B[43m)\u001B[49m\n",
      "File \u001B[1;32mE:\\ProgramData\\anaconda3\\envs\\chatglm3-qlora\\lib\\site-packages\\torch\\_tensor.py:487\u001B[0m, in \u001B[0;36mTensor.backward\u001B[1;34m(self, gradient, retain_graph, create_graph, inputs)\u001B[0m\n\u001B[0;32m    477\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m has_torch_function_unary(\u001B[38;5;28mself\u001B[39m):\n\u001B[0;32m    478\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m handle_torch_function(\n\u001B[0;32m    479\u001B[0m         Tensor\u001B[38;5;241m.\u001B[39mbackward,\n\u001B[0;32m    480\u001B[0m         (\u001B[38;5;28mself\u001B[39m,),\n\u001B[1;32m   (...)\u001B[0m\n\u001B[0;32m    485\u001B[0m         inputs\u001B[38;5;241m=\u001B[39minputs,\n\u001B[0;32m    486\u001B[0m     )\n\u001B[1;32m--> 487\u001B[0m \u001B[43mtorch\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mautograd\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mbackward\u001B[49m\u001B[43m(\u001B[49m\n\u001B[0;32m    488\u001B[0m \u001B[43m    \u001B[49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mgradient\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mretain_graph\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mcreate_graph\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43minputs\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43minputs\u001B[49m\n\u001B[0;32m    489\u001B[0m \u001B[43m\u001B[49m\u001B[43m)\u001B[49m\n",
      "File \u001B[1;32mE:\\ProgramData\\anaconda3\\envs\\chatglm3-qlora\\lib\\site-packages\\torch\\autograd\\__init__.py:200\u001B[0m, in \u001B[0;36mbackward\u001B[1;34m(tensors, grad_tensors, retain_graph, create_graph, grad_variables, inputs)\u001B[0m\n\u001B[0;32m    195\u001B[0m     retain_graph \u001B[38;5;241m=\u001B[39m create_graph\n\u001B[0;32m    197\u001B[0m \u001B[38;5;66;03m# The reason we repeat same the comment below is that\u001B[39;00m\n\u001B[0;32m    198\u001B[0m \u001B[38;5;66;03m# some Python versions print out the first line of a multi-line function\u001B[39;00m\n\u001B[0;32m    199\u001B[0m \u001B[38;5;66;03m# calls in the traceback and some print out the last line\u001B[39;00m\n\u001B[1;32m--> 200\u001B[0m \u001B[43mVariable\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_execution_engine\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mrun_backward\u001B[49m\u001B[43m(\u001B[49m\u001B[43m  \u001B[49m\u001B[38;5;66;43;03m# Calls into the C++ engine to run the backward pass\u001B[39;49;00m\n\u001B[0;32m    201\u001B[0m \u001B[43m    \u001B[49m\u001B[43mtensors\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mgrad_tensors_\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mretain_graph\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mcreate_graph\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43minputs\u001B[49m\u001B[43m,\u001B[49m\n\u001B[0;32m    202\u001B[0m \u001B[43m    \u001B[49m\u001B[43mallow_unreachable\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43;01mTrue\u001B[39;49;00m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43maccumulate_grad\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43;01mTrue\u001B[39;49;00m\u001B[43m)\u001B[49m\n",
      "\u001B[1;31mRuntimeError\u001B[0m: element 0 of tensors does not require grad and does not have a grad_fn"
     ]
    }
   ],
   "execution_count": 101
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "5503e0732df93e9f"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
